import jwt from 'jsonwebtoken';

export const authenticateToken = (req, res, next) => {
    const authHeader = req.headers['authorization'];
    const token = authHeader && authHeader.split(' ')[1];

    if (token == null)
         return res.sendStatus(401);

    jwt.verify(token, process.env.JWT_KEY as string, (err: any, jwtPayload: any) => {
        if (err) {
            console.error(`${new Date()} error: `, err);
            return res.sendStatus(403);
        }
        req.jwtPayload = jwtPayload;
        next()
    })
}
